import { Request, Response } from 'express';

// 模拟数据存储
let schedules: any[] = [];

/**
 * 获取所有发布计划
 */
export const getAllSchedules = (req: Request, res: Response) => {
  res.status(200).json({
    success: true,
    schedules
  });
};

/**
 * 创建新的发布计划
 */
export const createSchedule = (req: Request, res: Response) => {
  try {
    const { title, platform, content, date, time } = req.body;
    
    // 验证请求参数
    if (!title || !platform || !content || !date || !time) {
      return res.status(400).json({
        success: false,
        message: '请提供所有必要的参数'
      });
    }
    
    const newSchedule = {
      id: Date.now().toString(),
      title,
      platform,
      content,
      date,
      time,
      status: 'pending'
    };
    
    schedules.push(newSchedule);
    
    res.status(201).json({
      success: true,
      schedule: newSchedule
    });
  } catch (error) {
    console.error('创建发布计划失败:', error);
    res.status(500).json({
      success: false,
      message: '服务器内部错误'
    });
  }
};

/**
 * 更新发布计划
 */
export const updateSchedule = (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const updateData = req.body;
    
    const scheduleIndex = schedules.findIndex(s => s.id === id);
    
    if (scheduleIndex === -1) {
      return res.status(404).json({
        success: false,
        message: '未找到指定的发布计划'
      });
    }
    
    schedules[scheduleIndex] = {
      ...schedules[scheduleIndex],
      ...updateData
    };
    
    res.status(200).json({
      success: true,
      schedule: schedules[scheduleIndex]
    });
  } catch (error) {
    console.error('更新发布计划失败:', error);
    res.status(500).json({
      success: false,
      message: '服务器内部错误'
    });
  }
};

/**
 * 删除发布计划
 */
export const deleteSchedule = (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    
    const initialLength = schedules.length;
    schedules = schedules.filter(s => s.id !== id);
    
    if (schedules.length === initialLength) {
      return res.status(404).json({
        success: false,
        message: '未找到指定的发布计划'
      });
    }
    
    res.status(200).json({
      success: true,
      message: '发布计划已成功删除'
    });
  } catch (error) {
    console.error('删除发布计划失败:', error);
    res.status(500).json({
      success: false,
      message: '服务器内部错误'
    });
  }
}; 